<?

class UploadImage extends Button  {

    function render($sel) {
       return $this->renderDialogButton('uploadimage','Upload image','Upload and resize image');
    }

    function action($sel) {
        if (!isset($_POST['resize'])) {
            return;
        }
        if (is_uploaded_file($_FILES['newfile']['tmp_name']))  {
            list($new_width,$new_height) = explode('x',$_POST['resize']);
            list($width, $height) = getimagesize($_FILES['newfile']['tmp_name']);
            if (($width < $new_width) || ($height < $new_height)) {
				$new_width  = $width;
				$new_height = $height;
			} elseif (($new_width != 0) && ($new_height != 0)) {
                $x_ratio = $width / $new_width;
                $y_ratio = $height / $new_height;
                $ratio = max($x_ratio,$y_ratio);
                $new_width  = round($width / $ratio);
                $new_height = round($height / $ratio);
            } else {
                $new_height = $height;
                $new_width  = $width;
            }
            $ext = strtolower(substr($_FILES['newfile']['name'],strrpos($_FILES['newfile']['name'],'.')+1));
            $img_new = imagecreate($new_width,$new_height);
            switch($ext) {
                case 'gif' :
                    $img = imagecreatefromgif($_FILES['newfile']['tmp_name']);
                    if ($img) {
                       	imagecopyresampled($img_new, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                        imagegif($img_new,$sel->getCurrentDirName().'/'.basename($_FILES['newfile']['name']));
                    }
                    break;

                case 'jpg':
                case 'jpeg':
                    $img = imagecreatefromjpeg($_FILES['newfile']['tmp_name']);
                    if ($img) {
                        imagecopyresampled($img_new, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                        imagejpeg($img_new,$sel->getCurrentDirName().'/'.basename($_FILES['newfile']['name']));
                    }
                    break;

                case 'png':
                    $img = imagecreatefrompng($_FILES['newfile']['tmp_name']);
                    if ($img) {
                        imagecopyresampled($img_new, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                        imagepng($img_new,$sel->getCurrentDirName().'/'.basename($_FILES['newfile']['name']));
                    }
                    break;

                default:
                    header('Location: '.Config::getMsgLink($_GET['path'],MESSAGE_ERROR,'Not valid image!'));
                    exit;
            }
        }
        $f = new File($sel->getCurrentDirName().basename($_FILES['newfile']['name']));
        $old_f = new File($_FILES['newfile']['tmp_name']);
        header('Location: '.Config::getMsgLink($_GET['path'],basename($_FILES['newfile']['name']),MESSAGE_INFO,'File <b>'.basename($f->name).'</b> was successfully uploaded and resized: '."{$width}x{$height} (".$old_f->getSizeF().") -> {$new_width}x{$new_height} (".$f->getSizeF().")"));
    }

    function renderDialog($sel) {
        $IMAGERESIZE = Config::getValue('imageresize','IMAGERESIZE');
        $res = '<form enctype="multipart/form-data" method="POST" action="'.Config::getLink($_GET['path'],'','action=uploadimage').'"><b>File name: </b> <input type="file" name="newfile"> Resize to: <select name="resize">';
        foreach ($IMAGERESIZE as $size) {
            $res .= "<option value='{$size['width']}x{$size['height']}'>{$size['name']} ({$size['width']}x{$size['height']} px)";
        }
        $res .= '<option value="0x0">no resize</select><input type="submit" value="Upload"></form>';
        return $res;
    }

}

?>